<template>
  <div class="p-radio-group">
    <div
      class="u-item"
      v-for="item in list"
      :class="{ 'u-actived': modelValue === item[valueKey] }"
      @click="() => clickItem(item[valueKey])"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script setup name="RadioGroup">
/**
 * 使用：
 * <RadioGroup :list="radioList" v-model="radioValue"></RadioGroup>
 */
defineProps({
  modelValue: [String, Number, Boolean, Object],
  list: Array, // [{ label: string, value: any }]
  valueKey: {
    // value对应的key
    type: String,
    default: 'value',
  },
})

const emit = defineEmits(['update:modelValue', 'change'])

function clickItem(v) {
  emit('update:modelValue', v)
  emit('change', v)
}
</script>

<style lang="scss" scoped>
.p-radio-group {
  display: flex;
  flex-wrap: wrap;
  .u-item {
    // element表单项行高32px，所以这里增加了上下边距
    margin: 5px 12px 5px 0;
    padding: 0 8px;
    line-height: 22px;
    font-size: 14px;
    color: #000;
    border-radius: 2px;
    cursor: pointer;
    &.u-actived {
      background-color: rgba(59, 104, 240, 0.1);
      color: #3b68f0;
    }
  }
}
</style>
